package com.kaifamiao.datastreams;

import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;

public class DataInputTest {

    public static void main(String[] args) throws IOException {

        final byte[] nameBytes = new byte[30];

        InputStream in = new FileInputStream("afang.data");
        DataInputStream dis = new DataInputStream(in);

        int id = dis.readInt(); // 读取4个字节并重构int数值
        System.out.println( id );

        dis.read(nameBytes); // 读取 nameBytes.length 个字节到数组中
        String name = new String(nameBytes, StandardCharsets.UTF_8);
        System.out.println( name );

        char gender = dis.readChar(); // 读取2个字节并重构char数值
        System.out.println( gender );

        boolean married = dis.readBoolean(); // 读取1个字节并重构boolean数值
        System.out.println( married );

        short year = dis.readShort(); // 读取2个字节并重构short数值
        byte month = dis.readByte(); // 读取1个字节并重构byte数值
        byte date = dis.readByte();
        System.out.printf("%d-%d-%d\n", year, month, date );

        dis.close();
        in.close();
    }
}
